package cn.e3mall.sso.service.impl;

import cn.e3mall.common.jedis.JedisClient;
import cn.e3mall.common.utils.E3Result;
import cn.e3mall.pojo.TbUser;
import cn.e3mall.sso.service.TokenService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class TokenServiceImpl implements TokenService {

    @Value("${SESSION_EXPIRE}")
    private Integer SESSION_EXPIRE;
    
    @Autowired
    private JedisClient jedisClient;
    @Override
    public E3Result getUserByToken(String token) {
        String s = jedisClient.get("SESSION:" + token);
        if(StringUtils.isEmpty(s)){
          return E3Result.build(201,"登陆信息过期");
        }else {
            jedisClient.expire("SESSION:" + token,SESSION_EXPIRE);
        }
        TbUser tbUser = JSONObject.parseObject(s, TbUser.class);
        return E3Result.ok(tbUser);
    }
}
